FIND_PACKAGE(OpenGLES REQUIRED)

IF(NOT WIN32)
  IF(APPLE)
    FIND_LIBRARY(CARBON NAMES Carbon)
    FIND_LIBRARY(COCOA NAMES Cocoa)
  ELSE(APPLE)
    FIND_PACKAGE(X11)
    FIND_PACKAGE(XF86VidMode)
  ENDIF(APPLE)
ENDIF(NOT WIN32)

SET(SOURCE_DIR ${CMAKE_SOURCE_DIR}/nel/src/3d/driver/opengl)

FILE(GLOB SRC ${SOURCE_DIR}/*.cpp ${SOURCE_DIR}/*.h ${SOURCE_DIR}/*.def)

IF(APPLE)
  FILE(GLOB MAC_SRC ${SOURCE_DIR}/mac/*.h ${SOURCE_DIR}/mac/*.m ${SOURCE_DIR}/mac/*.mm ${SOURCE_DIR}/mac/*.cpp)
  SET(SRC ${SRC} ${MAC_SRC})
  SET_SOURCE_FILES_PROPERTIES(${SRC} PROPERTIES COMPILE_FLAGS "-x objective-c++")
ENDIF(APPLE)

INCLUDE_DIRECTORIES(${SOURCE_DIR} ${OPENGLES_INCLUDE_DIR})

ADD_DEFINITIONS(-DUSE_OPENGLES)

IF(WIN32)
  SET(NLDRV_OGLES_LIB "nel_drv_opengles_win")
ELSE(WIN32)
  SET(NLDRV_OGLES_LIB "nel_drv_opengles")
ENDIF(WIN32)

NL_TARGET_DRIVER(${NLDRV_OGLES_LIB} ${SRC})

TARGET_LINK_LIBRARIES(${NLDRV_OGLES_LIB} nel3d nelmisc ${OPENGLES_LIBRARIES})
NL_DEFAULT_PROPS(${NLDRV_OGLES_LIB} "NeL, Driver, Video: OpenGL ES")
NL_ADD_LIB_SUFFIX(${NLDRV_OGLES_LIB})
NL_ADD_RUNTIME_FLAGS(${NLDRV_OGLES_LIB})

IF(WIN32)
  INCLUDE_DIRECTORIES(${DXSDK_INCLUDE_DIR})
  TARGET_LINK_LIBRARIES(${NLDRV_OGLES_LIB} ${DXSDK_DINPUT_LIBRARY} ${DXSDK_GUID_LIBRARY})
  ADD_DEFINITIONS(/DDRIVER_OPENGLES_EXPORTS)
ENDIF(WIN32)

IF(APPLE)
  TARGET_LINK_LIBRARIES(${NLDRV_OGLES_LIB} ${CARBON})
  TARGET_LINK_LIBRARIES(${NLDRV_OGLES_LIB} ${COCOA})
ENDIF(APPLE)

IF(UNIX AND NOT APPLE)
  TARGET_LINK_LIBRARIES(${NLDRV_OGLES_LIB} ${X11_X11_LIB})
  IF(XF86VidMode_FOUND)
    INCLUDE_DIRECTORIES(${XF86VidMode_INCLUDE_DIR})
    ADD_DEFINITIONS(${XF86VidMode_DEFINITIONS})
    TARGET_LINK_LIBRARIES(${NLDRV_OGLES_LIB} ${XF86VidMode_LIBRARY})
  ENDIF(XF86VidMode_FOUND)
  IF(X11_Xrandr_FOUND)
    INCLUDE_DIRECTORIES(${X11_Xrandr_INCLUDE_PATH})
    ADD_DEFINITIONS(-DHAVE_XRANDR)
    TARGET_LINK_LIBRARIES(${NLDRV_OGLES_LIB} ${X11_Xrandr_LIB})
  ENDIF(X11_Xrandr_FOUND)
  IF(X11_Xrender_FOUND)
    INCLUDE_DIRECTORIES(${X11_Xrender_INCLUDE_PATH})
    ADD_DEFINITIONS(-DHAVE_XRENDER)
    TARGET_LINK_LIBRARIES(${NLDRV_OGLES_LIB} ${X11_Xrender_LIB})
  ENDIF(X11_Xrender_FOUND)
  IF(X11_Xcursor_FOUND)
    INCLUDE_DIRECTORIES(${X11_Xcursor_INCLUDE_PATH})
    ADD_DEFINITIONS(-DHAVE_XCURSOR)
    TARGET_LINK_LIBRARIES(${NLDRV_OGLES_LIB} ${X11_Xcursor_LIB})
  ENDIF(X11_Xcursor_FOUND)
ENDIF(UNIX AND NOT APPLE)

IF(WITH_PCH)
  ADD_NATIVE_PRECOMPILED_HEADER(${NLDRV_OGLES_LIB} ${SOURCE_DIR}/stdopengl.h ${SOURCE_DIR}/stdopengl.cpp)
ENDIF(WITH_PCH)

IF((WITH_INSTALL_LIBRARIES AND WITH_STATIC_DRIVERS) OR NOT WITH_STATIC_DRIVERS)
  INSTALL(TARGETS ${NLDRV_OGLES_LIB} LIBRARY DESTINATION ${NL_DRIVER_PREFIX} ARCHIVE DESTINATION ${NL_LIB_PREFIX} RUNTIME DESTINATION ${NL_DRIVER_PREFIX} COMPONENT drivers3d)
  IF(WITH_MAXPLUGIN)
    INSTALL(TARGETS ${NLDRV_OGLES_LIB} RUNTIME DESTINATION maxplugin COMPONENT drivers3d)
  ENDIF(WITH_MAXPLUGIN)
ENDIF((WITH_INSTALL_LIBRARIES AND WITH_STATIC_DRIVERS) OR NOT WITH_STATIC_DRIVERS)
